package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetFreewayApply;
import com.jt.tenant.dialog.ResultDialog;
import com.jt.tenant.dialog.TipDialog;
import com.jt.tenant.model.PicModel;
import com.jt.tenant.viewpager.ImagePagerActivity;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by AD on 2016/5/12.
 */
public class UpPictureActivity extends JTActivity implements OnClickListener,View.OnLongClickListener {

    private TextView title_name1, bottom_text;

    private ImageView iv_new_ins_card_face, iv_new_ins_card_back;

    private ImageView iv_new_ins_license_face, iv_new_ins_license_back;

    private ImageView iv_new_ins_vtl_face, iv_new_ins_vtl_back;

    private List<PicModel> mPersionLists = new ArrayList<>();

    private List<ImageView> image = new ArrayList<>();

    private List<PicModel> mPicLists = new ArrayList<>();

    public static SetRefrencePicListener mRefrenceListener;

    public static DeletePic deletePic;

    private TipDialog tipDialog;

    private int deletePosition = -1;

    private TextView textViewBottom;

    private int position = 0;

    private SetFreewayApply setFreewayApply = new SetFreewayApply(new AsyCallBack<SetFreewayApply.Info>(){

        @Override
        public void onSuccess(String toast, int type, SetFreewayApply.Info info) throws Exception {

            super.onSuccess(toast, type, info);

            if(info.msg.equals("000")){

                TipDialog dialog = new TipDialog(context);

                dialog.setTip("您的服务申请已提交，请耐心等待审核。");

                dialog.setType(11);

                JTApplication.jtPreferences.saveFreeway("0");

                dialog.show();

            }else {

                UtilToast.show(context,info.msg);

            }

        }
    });



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_uppicture);


        for (int i = 0; i < 6; i++) {

            PicModel defaultModel = new PicModel();

            defaultModel.picUrl = "";

            defaultModel.isPic = false;

            mPersionLists.add(defaultModel);
        }

        initView();

    }

    private void initView() {

        (title_name1 = (TextView) findViewById(R.id.tv_title_name)).setText("上传证件信息");

        (iv_new_ins_card_face = (ImageView) findViewById(R.id.iv_new_ins_card_face)).setOnClickListener(this);
        iv_new_ins_card_face.setOnLongClickListener(this);
        image.add(iv_new_ins_card_face);

        (iv_new_ins_card_back = (ImageView) findViewById(R.id.iv_new_ins_card_back)).setOnClickListener(this);
        iv_new_ins_card_back.setOnLongClickListener(this);
        image.add(iv_new_ins_card_back);

        (iv_new_ins_license_face = (ImageView) findViewById(R.id.iv_new_ins_license_face)).setOnClickListener(this);
        iv_new_ins_license_face.setOnLongClickListener(this);
        image.add(iv_new_ins_license_face);

        (iv_new_ins_license_back = (ImageView) findViewById(R.id.iv_new_ins_license_back)).setOnClickListener(this);
        iv_new_ins_license_back.setOnLongClickListener(this);
        image.add(iv_new_ins_license_back);

        (iv_new_ins_vtl_face = (ImageView) findViewById(R.id.iv_new_ins_vtl_face)).setOnClickListener(this);
        iv_new_ins_vtl_face.setOnLongClickListener(this);
        image.add(iv_new_ins_vtl_face);

        (iv_new_ins_vtl_back = (ImageView) findViewById(R.id.iv_new_ins_vtl_back)).setOnClickListener(this);
        iv_new_ins_vtl_back.setOnLongClickListener(this);
        image.add(iv_new_ins_vtl_back);

//        for(int i = 0;i<image.size();i++){
//
//            if(i%2 == 1){
//
//                image.get(i).setVisibility(View.GONE);
//
//            }
//
//        }


        textViewBottom = (TextView) findViewById(R.id.bottom_text);

        textViewBottom.setText("提交");

        textViewBottom.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                int p = 0;

                for(int i = 0;i<image.size();i++){

                    if(mPersionLists.get(i).picUrl.equals("")){

                        p = 1;

                    }

                }

                if(p != 1) {

                    setFreewayApply.userName = getIntent().getStringExtra("name");

                    setFreewayApply.sex = getIntent().getStringExtra("sex");

                    setFreewayApply.idCard = getIntent().getStringExtra("card_id");

                    setFreewayApply.driverNum = getIntent().getStringExtra("card_id");

                    setFreewayApply.carNum = getIntent().getStringExtra("plate_num");

                    setFreewayApply.address = getIntent().getStringExtra("address");

                    setFreewayApply.woPayCode = getIntent().getStringExtra("wo_account");

                    setFreewayApply.idCardList = mPersionLists.get(0).picUrl+","+mPersionLists.get(1).picUrl;

                    setFreewayApply.driverNumList = mPersionLists.get(2).picUrl+","+mPersionLists.get(3).picUrl;

                    setFreewayApply.carNumList = mPersionLists.get(4).picUrl+","+mPersionLists.get(5).picUrl;

                    setFreewayApply.execute(context);


                }else {

                    UtilToast.show(context,"请上传完整照片信息");

                }

            }
        });

        mRefrenceListener = new SetRefrencePicListener() {
            @Override
            public void setRefrencePic(String imageUrl) {

               mPersionLists.get(position).picUrl = imageUrl;

                mPersionLists.get(position).isPic = true;

                JTApplication.UtilAsyncBitmap.get(mPersionLists.get(position).picUrl,image.get(position));

                if(position%2==0){

                    image.get(position+1).setVisibility(View.VISIBLE);

//                    image.get(position+1).setImageResource(R.mipmap.add_pic);

                }


            }
        };


        deletePic = new DeletePic() {
            @Override
            public void delete() {

                    mPersionLists.get(deletePosition).picUrl = "";

                    mPersionLists.get(deletePosition).isPic = false;

                if(deletePosition == 0||deletePosition == 2||deletePosition == 4){

                    image.get(deletePosition).setImageResource(R.mipmap.face_bg);

                    }else{

                    image.get(deletePosition).setImageResource(R.mipmap.back_bg);

                    }


            }
        };

    }

    @Override
    public void onClick(View v) {

        int position = getID(v);

            if (!mPersionLists.get(position).isPic)

                startActivity(new Intent(UpPictureActivity.this, ChoosePicActivity.class)

                        .putExtra("type","1")

                );

            else {

                mPicLists.clear();

                for(int i=0;i<mPersionLists.size();i++){

                    if(mPersionLists.get(i).isPic){

                        mPicLists.add(mPersionLists.get(i));

                    }

                }

                Intent intent = new Intent(UpPictureActivity.this, ImagePagerActivity.class);

                Bundle bundle = new Bundle();

                bundle.putInt(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);

                bundle.putSerializable(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) mPicLists);

                bundle.putString("flag", "2");

                intent.putExtras(bundle);

                startActivity(intent);

            }



    }

    @Override
    public boolean onLongClick(View v) {

        int position = getID(v);

        if (mPersionLists.get(position).isPic) {
            deletePosition = position;
            TipDialog dialog = new TipDialog(context);
            dialog.setType(10);
            dialog.setTip("您确定要删除此张图片么？");
            dialog.show();
        }

        return true;

    }

    /***
     * 设置选择完图片刷新Grid
     */
    public abstract class SetRefrencePicListener {

        public abstract void setRefrencePic(String imageUrl);

    }

    public abstract class DeletePic {

        public abstract void delete();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mRefrenceListener = null;

        deletePic = null;

    }

    private int getID(View v) {

        switch (v.getId()) {

            case R.id.iv_new_ins_card_face:

                position = 0;

                return 0;

            case R.id.iv_new_ins_card_back:

                position = 1;

                return 1;

            case R.id.iv_new_ins_license_face:

                position = 2;

                return 2;


            case R.id.iv_new_ins_license_back:

                position = 3;

                return 3;

            case R.id.iv_new_ins_vtl_face:

                position = 4;

                return 4;

            case R.id.iv_new_ins_vtl_back:

                position = 5;

                return 5;

            default:

                return -1;

        }

    }
}
